using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MyStack.Models;

namespace MyStack.Controllers
{
    public class StoryController : Controller
    {
        //
        // GET: /Story/

        public ActionResult Index()
        {
			return RedirectToAction("Explore");
        }

		public ActionResult Explore()
		{
			// get list of story histories
			MyStackRepository db = new MyStackRepository();
			StoriesView[] stories = db.GetAllStories();
			return View("Explore", stories);
		}

        public ActionResult Popular()
        {
            MyStackRepository db = new MyStackRepository();
            StoryView[] stories = db.GetAllPopularStories();
            return View("Popular", stories);
        }

        public ActionResult View(int id)
        {
			MyStackRepository db = new MyStackRepository();

			StoryView story = db.GetStoryView(id);

            return View(story);
        }

        public ActionResult History(int id)
        {
            MyStackRepository db = new MyStackRepository();
            
            // check if the story exists
            try
            {
                db.GetStory(id);
            }
            catch
            {
                return View("Index");
            }

            StoriesView[] stories = db.getStoryHistory(id);
            return View("History", stories);
        }
    }
}
